package Net.tcp.chat4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 在线聊天室：服务器端
 * 目标：是在私聊最终版
 */
public class TMoreChat {
    private static CopyOnWriteArrayList<Channel> all = new CopyOnWriteArrayList<>();
    public static void main(String[] args) throws IOException {
        System.out.println("----Server----");
        //1.指定端口 使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        // 2.阻塞式等待链接accept
        System.out.println("客户端建立了一个连接");
        while (true){
            Socket client = server.accept();
            Channel c = new Channel(client);
            all.add(c);//管理所有的成员
            new Thread(c).start();
        }
    }

    static class Channel implements Runnable{
        private DataInputStream dis = null;//getInputStream返回此套接字的输入流。
        private DataOutputStream dos = null;//getOutputStream返回此套接字的输出流。
        private Socket client;
        private boolean isRunning = false;
        private String name;
        public Channel(Socket client){
            this.client=client;
            try {
                dis=new DataInputStream(client.getInputStream());
                dos=new DataOutputStream(client.getOutputStream());
                //接收用户名
                this.name = Receive();
                isRunning = true;
            } catch (IOException e) {
                release();
            }
        }
        //接收消息
        private String Receive(){
            String msg="";
            try {
                msg = dis.readUTF();
                return msg;
            } catch (IOException e) {
               release();
            }
            return msg;
        }
        //发送消息
        private void send(String msg){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
               release();
            }
        }
        //群聊
        //私聊：约定数据格式：@xx:msg
        private void sendOther(String msg){
            boolean isPrivate = msg.startsWith("@");//是否是@开头
            if(isPrivate){//私聊
                int idx = msg.indexOf(":");
                //获取目标和数据
                String targetName = msg.substring(1,idx);
                msg = msg.substring(idx+1);
                for (Channel other:all
                        ) {
                    if(other.name.equals(targetName)){//找到目标
                        other.send(name+"悄悄对您说："+msg);
                        break;
                    }
                }
            }else {//群聊
                for (Channel other:all
                        ) {
                    if(other==this){//自己
                        continue;
                    }
                    other.send(name+"："+msg);
                }
            }
        }
        //释放资源
        private void release(){
            this.isRunning=false;
            Utils.close(dis,dos,client);
            //退出
            sendOther("我回家冲浪去了");
        }

        @Override
        public void run() {
            while (isRunning){
                String msg = Receive();
                if(!msg.equals("")){
                    //send(msg);
                    sendOther(msg);
                }
            }
        }
    }
}
